package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TProdInv;
import com.ruoyi.system.domain.TProdInvRecord;
import com.ruoyi.system.domain.TProdRecord;
import com.ruoyi.system.domain.dto.TProdRecordDTO;
import com.ruoyi.system.domain.vo.PieVO;
import com.ruoyi.system.domain.vo.ProdYearTotalVO;
import com.ruoyi.system.domain.vo.TProdRecordVO;
import com.ruoyi.system.domain.vo.TProdTaskVO;
import com.ruoyi.system.mapper.TProdRecordMapper;
import com.ruoyi.system.service.ITProdInvRecordService;
import com.ruoyi.system.service.ITProdInvService;
import com.ruoyi.system.service.ITProdRecordService;
import com.ruoyi.system.service.ITProdTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wux
 * @date 2023/5/30 9:40
 */
@Service
public class TProdRecordServiceImpl extends ServiceImpl<TProdRecordMapper, TProdRecord> implements ITProdRecordService {
    @Autowired
    private ITProdTaskService prodTaskService;
    @Autowired
    private ITProdInvService prodInvService;
    @Autowired
    private ITProdInvRecordService prodInvRecordService;

    @Override
    public List<TProdRecordVO> listByPage(TProdRecordDTO params) {
        if(!StringUtils.isBlank(params.getStartDate())){
            params.setStartDate(params.getStartDate() + " 00:00:00");
        }
        if(!StringUtils.isBlank(params.getEndDate())){
            params.setEndDate(params.getEndDate() + " 23:59:59");
        }
        PageUtil.startPage(params);
        return this.baseMapper.listByPage(params);
    }

    @Override
    @Transactional
    public void add(TProdRecord params) {
//        TProdTask prodTask = prodTaskService.getOne(new LambdaQueryWrapper<TProdTask>()
//                .eq(TProdTask :: getTaskCode, params.getTaskCode()));
//        if(Objects.isNull(prodTask)){
//            throw new CustomException(params.getTaskCode() + "生产单不存在!");
//        }
        //新增生产记录
        // TODO 生产生产记录号
        this.save(params);
        //更新库存表
        TProdInv prodInv = prodInvService.getOne(new LambdaQueryWrapper<TProdInv>()
                .eq(TProdInv :: getProdId, params.getProdId()));
        if(Objects.isNull(prodInv)){
            prodInv = new TProdInv();
            prodInv.setProdId(params.getProdId());
            prodInv.setQuantity(params.getFinishNum());
            prodInv.setInventory(params.getFinishNum());
        }else {
            prodInv.setQuantity(prodInv.getQuantity() + params.getFinishNum());
            prodInv.setInventory(prodInv.getInventory() + params.getFinishNum());
        }
        prodInvService.saveOrUpdate(prodInv);
        //进行入库操作
        TProdInvRecord invRecord = new TProdInvRecord();
        invRecord.setInvId(prodInv.getId());
        invRecord.setType(1);
        invRecord.setOperateNum(params.getFinishNum());
//        invRecord.setBatchCode(params.getBatchCode());
        invRecord.setCreateBy(1L);
        invRecord.setUpdateBy(1L);
        prodInvRecordService.save(invRecord);
    }

    @Override
    public TProdTaskVO getRecordByProdId(Long prodId) {
        return this.baseMapper.getRecordByProd(prodId);
    }

    @Override
    public int sumRecord(Long projectId) {
        return this.baseMapper.sumRecord(projectId);
    }

    @Override
    public int sumYearRecord(Long projectId) {
        return this.baseMapper.sumYearRecord(projectId);
    }

    @Override
    public int sumMonthRecord(Long projectId) {
        return this.baseMapper.sumMonthRecord(projectId);
    }

    @Override
    public int sumDayRecord(Long projectId) {
        return this.baseMapper.sumDayRecord(projectId);
    }

    @Override
    public List<PieVO> groupProd(Long projectId) {
        return this.baseMapper.groupProd(projectId);
    }

    @Override
    public List<ProdYearTotalVO> groupYear(Long projectId) {
        return this.baseMapper.groupYear(projectId);
    }

    @Override
    public List<PieVO> groupMonth(Long projectId, String year) {
        return this.baseMapper.groupMonth(projectId,year);
    }

    @Override
    public List<PieVO> groupDay(Long projectId, String monthDate) {
        return this.baseMapper.groupDay(projectId,monthDate);
    }

    @Override
    public int sumXYearRecord(Long projectId, String year) {
        return this.baseMapper.sumXYearRecord(projectId,year);
    }

    @Override
    public int sumXMonthRecord(Long projectId, String monthDate) {
        return this.baseMapper.sumXMonthRecord(projectId,monthDate);
    }

    @Override
    public Map<String,Object> getTaskRecordByDay() {
        List<String> last7Days = DateUtils.getLast7Days();
        List<Map<String,Object>> list = baseMapper.getTaskRecordByDay();
        Map<String,Object> result = new HashMap<>();
        Map<String,List<Map<String,Object>>> groupMap = list.stream().collect(Collectors.groupingBy(item -> item.get("prodType").toString()));
        groupMap.forEach((k,v) -> {
            List<Map<String,Object>> temp = new ArrayList<>();
            last7Days.stream().forEach(day -> {
                Boolean contains = false;
                for(Map<String,Object> vc : v){
                    if(vc.get("madeDate").equals(day)){
                        contains = true;
                        temp.add(vc);
                    }
                }
                if(!contains){
                    Map<String,Object> tempMap = new HashMap<>();
                    tempMap.put("madeDate",day);
                    tempMap.put("prodType",k);
                    tempMap.put("finishNum",0);
                    temp.add(tempMap);
                }
            });
            result.put(k,temp);
        });
        return result;
    }

    @Override
    public List<TProdRecordVO> getByBatchCode(TProdRecordDTO params) {
        return baseMapper.getByBatchCode(params);
    }

    @Override
    public List<TProdRecordVO> getByBatchCode2(TProdRecordDTO params) {
        return baseMapper.getByBatchCode2(params);
    }

    @Override
    public int sumByProd(Long prodId) {
        return baseMapper.sumByProd(prodId);
    }
}
